package 算法.jt;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * JDK动态代理机制只能代理实现了接口的类
 *
 * @author lchenglong
 * @date 2020/12/12
 */
public class CountProxyForDynamic implements InvocationHandler {
    private Object target;

    public Object bind(Object target) {
        this.target = target;
        // 取得代理对象
        // 需要绑定一个接口，cglib弥补了这个缺陷
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        System.out.println("开始执行方法");
        result = method.invoke(target, args);
        System.out.println("执行方法结束");
        return result;
    }
}
